//
//  TZHallCell.m
//  wangYiLottery
//
//  Created by mac on 16/2/18.
//  Copyright © 2016年 tz. All rights reserved.
//

#import "TZHallCell.h"
#import "TZHallCellModel.h"
#import "DCPicScrollView.h"
#import "DCWebImageManager.h"
#import "TZButtonModel.h"
#import "TZBtnCell.h"
@interface TZHallCell ()
@property (weak, nonatomic) IBOutlet UIView *scrollView;


@property (nonatomic, copy) NSString *picture;
<<<<<<< HEAD
@property (nonatomic, strong) NSMutableArray *mArray;
@property (nonatomic, strong) UIView *backView;
=======
@property (nonatomic, strong) TZBtnCell *backView;
//@property (nonatomic, strong) UIButton *didButton;
//@property (nonatomic, strong) UILabel *didLabel;
>>>>>>> b46e790f68e037a72e1c63bed0aa6db322c7b047
@end
@implementation TZHallCell
#define KBUTTON_COUNT 8 //控件总数
#define KROW_COUNT 4 //每行控件数

//cell重用的button的点击事件
- (void)button1DidClick {
    NSLog(@"1");
}

- (void)button2DidClick {
    NSLog(@"2");
}

- (void)button3DidClick {
    NSLog(@"3");
}

- (void)setModel:(TZHallCellModel *)model {
    _model = model;
    
}

- (void)setButtonModel:(TZButtonModel *)buttonModel {
    _buttonModel = buttonModel;
<<<<<<< HEAD
    self.backView.backgroundColor = [UIColor redColor];
=======
    if ([self.model.title isEqualToString:@"1"]) {
            self.didButton.backgroundColor = [UIColor redColor];
//        self.didLabel.text = @"111";
////        [self.didButton setBackgroundImage:[UIImage imageNamed:buttonModel.picture] forState:UIControlStateNormal];
////        self.didLabel.text = buttonModel.title;
    }
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//                    self.backView = [[UIView alloc] init];
//            [self addSubview:self.backView];
//            self.didButton = [[UIButton alloc] init];
//            [self.backView addSubview:self.didButton];
//            self.didLabel = [[UILabel alloc] init];
//            [self.backView addSubview:self.didLabel];
        
    }
    return self;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
>>>>>>> b46e790f68e037a72e1c63bed0aa6db322c7b047
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self setupUI];
}

//更新UI
- (void)setupUI {
    if ([self.model.title isEqualToString:@"0"]) {
        //无限轮播
        self.scrollView = [self addScrollViewInCell];
    } else if ([self.model.title isEqualToString:@"1"]) {
        //设置Button
        [self settingButtonInCell];
        
    } else if ([self.model.title isEqualToString:@"2"]) {
        //cell重用
        [self settingCellInTableView];
    }
}

//---------------- 华丽分割线 -----------------------
- (void)settingButtonInCell {
    //存tag值
    self.mArray = [NSMutableArray arrayWithCapacity:KBUTTON_COUNT];
    self.backView = [[UIView alloc] init];
    //View
    CGFloat view_W = 70;
    CGFloat view_H = 80;//self.frame.size.height / 2;
    CGFloat manger = (KSCREEN_WIDTH - KROW_COUNT * view_W) / (KROW_COUNT + 1);
    CGFloat margin_top = 10;
    for (NSInteger i = 0; i < KBUTTON_COUNT; i ++) {
<<<<<<< HEAD
        UIView *backView = [[UIView alloc] init];
=======
//        self.backView = [[UIView alloc] init];
>>>>>>> b46e790f68e037a72e1c63bed0aa6db322c7b047
        NSInteger row = i / KROW_COUNT;
        NSInteger col = i % KROW_COUNT;
        CGFloat view_X = manger + col * (view_W + manger);
        CGFloat view_Y = row * (view_H + margin_top);
<<<<<<< HEAD
        backView.frame = CGRectMake(view_X, view_Y, view_W, view_H);
        [self.contentView addSubview:backView];
                NSInteger num = arc4random_uniform(255);
                backView.backgroundColor =[UIColor colorWithRed:num / 255.0 green:num / 255.0 blue:num / 255.0 alpha:1.0];
        //button
        UIImageView *didImageView = [[UIImageView alloc] init];
        [backView addSubview:didImageView];
        [didImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(backView.mas_height).multipliedBy(0.7);
            make.width.mas_equalTo(backView);
            make.top.equalTo(backView);
            make.centerX.equalTo(backView.mas_centerX);
        }];
        didImageView.tag = i;
        didImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"event_qiang_cell_tag_%zd",i + 1]];
        //label
        UILabel *didLabel = [[UILabel alloc] init];
        [backView addSubview:didLabel];
        [didLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(didImageView.mas_bottom);
            make.bottom.left.right.equalTo(backView);
        }];
        didLabel.backgroundColor = [UIColor yellowColor];
        //点击事件
        UITapGestureRecognizer *fingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fingerDidClick:)];
        [backView addGestureRecognizer:fingerOne];
//        self.backView = backView;
        self.backView.tag = i;
        //保存cell的个数便于判断跳转顺序
        NSString *tag = [NSString stringWithFormat:@"%zd",self.backView.tag];
        [self.mArray addObject:tag];
//        NSLog(@"%zd",self.backView.tag);
    }
//    NSLog(@"%zd",self.backView.tag);
=======
        self.backView = [[NSBundle mainBundle] loadNibNamed:@"TZBtnCell" owner:nil options:nil].firstObject;
        self.backView.frame = CGRectMake(view_X, view_Y, view_W, view_H);
        [self.contentView addSubview:self.backView];
        NSInteger num = arc4random_uniform(255);
        self.backView.backgroundColor =[UIColor colorWithRed:num / 255.0 green:num / 255.0 blue:num / 255.0 alpha:1.0];
        //button
//        self.didButton = [[UIButton alloc] init];
//        [self.backView addSubview:self.didButton];
//        [self.didButton mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.height.mas_equalTo(70);
//            make.width.mas_equalTo(10);
//            make.top.equalTo(self.backView);
//            make.centerX.equalTo(self.backView.mas_centerX);
//        }];
//        self.didButton.tag = i;
        [self.backView.didBtn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"event_qiang_cell_tag_%zd",i + 1]] forState:UIControlStateNormal];
//        NSLog(@"%@",self.backView.subviews.lastObject);
        
//        //监听事件
//        [self.didButton addTarget:self action:@selector(buttonDidClick) forControlEvents:UIControlEventTouchUpInside];
//        //label
////        self.didLabel = [[UILabel alloc] init];
////        [self.backView addSubview:self.didLabel];
//        [self.didLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(self.didButton.mas_bottom);
//            make.bottom.left.right.equalTo(self.backView);
//        }];
//        self.didLabel.backgroundColor = [UIColor yellowColor];
        }
>>>>>>> b46e790f68e037a72e1c63bed0aa6db322c7b047
}
- (void)fingerDidClick:(UIImageView *)imageView {
    //创建通知中心
    [[NSNotificationCenter defaultCenter] postNotificationName:@"fingerOneDidClick" object:self.mArray];
}

//---------------- 华丽分割线 -----------------------
//无限轮播
- (UIView *)addScrollViewInCell {
    //网络加载
<<<<<<< HEAD
    NSArray *UrlStringArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"TZHallHeader.plist" ofType:nil]];
    //    NSArray *titleArray = [@"午夜寂寞 谁来陪我,唱一首动人的情歌.你问我说 快不快乐,唱情歌越唱越寂寞.谁明白我 想要什么,一瞬间释放的洒脱.灯光闪烁 不必啰嗦,我就是传说中的那个摇摆哥.我是摇摆哥 音乐会让我快乐,我是摇摆哥 我已忘掉了寂寞.我是摇摆哥 音乐会让我洒脱,我们一起唱这摇摆的歌" componentsSeparatedByString:@"."];
=======
    
//    NSArray *UrlStringArray = @[@"http://pimg1.126.net/caipiao_info/images/headFigure/indexPage/1457938485624_1.png?24327636",
//                                @"http://pimg1.126.net/caipiao_info/images/headFigure/indexPage/1458723878000_1.jpg?24327636",
//                                @"http://pimg1.126.net/caipiao_info/images/headFigure/indexPage/1459407240622_1.jpg?24327636",
//                                @"http://pimg1.126.net/caipiao_info/images/headFigure/indexPage/1459243585164_1.jpg?24327636"];
 
    NSArray *UrlStringArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"TZHallHeader.plist" ofType:nil]];
//    NSURL *url = [NSURL URLWithString:self.headerModel.picture];
//    NSArray *UrlStringArray = [NSArray arrayWithContentsOfURL:url];
//    NSArray *UrlStringArray =
//    NSArray *titleArray = [@"午夜寂寞 谁来陪我,唱一首动人的情歌.你问我说 快不快乐,唱情歌越唱越寂寞.谁明白我 想要什么,一瞬间释放的洒脱.灯光闪烁 不必啰嗦,我就是传说中的那个摇摆哥.我是摇摆哥 音乐会让我快乐,我是摇摆哥 我已忘掉了寂寞.我是摇摆哥 音乐会让我洒脱,我们一起唱这摇摆的歌" componentsSeparatedByString:@"."];
    
>>>>>>> b46e790f68e037a72e1c63bed0aa6db322c7b047
    //显示顺序和数组顺序一致
    //设置图片url数组,和滚动视图位置
    
    DCPicScrollView  *picView = [DCPicScrollView picScrollViewWithFrame:self.frame WithImageUrls:UrlStringArray];
    //显示顺序和数组顺序一致
    //设置标题显示文本数组
    //    picView.titleData = titleArray;
    
    //占位图片,你可以在下载图片失败处修改占位图片
    picView.placeImage = [UIImage imageNamed:@"place.png"];
    
    //图片被点击事件,当前第几张图片被点击了,和数组顺序一致
    
    [picView setImageViewDidTapAtIndex:^(NSInteger index) {
        printf("第%zd张图片\n",index);
    }];
    
    //default is 2.0f,如果小于0.5不自动播放
    picView.AutoScrollDelay = 1.0f;
<<<<<<< HEAD
=======
    //    picView.textColor = [UIColor redColor];
    
>>>>>>> b46e790f68e037a72e1c63bed0aa6db322c7b047
    [self.contentView addSubview:picView];
    
    //下载失败重复下载次数,默认不重复,
    [[DCWebImageManager shareManager] setDownloadImageRepeatCount:1];
    
    //图片下载失败会调用该block(如果设置了重复下载次数,则会在重复下载完后,假如还没下载成功,就会调用该block)
    //error错误信息
    //url下载失败的imageurl
    [[DCWebImageManager shareManager] setDownLoadImageError:^(NSError *error, NSString *url) {
        NSLog(@"%@",error);
    }];
    return picView;
}
//---------------- 华丽分割线 -----------------------
//cell重用
- (void)settingCellInTableView {
    UIButton *button1 = [[UIButton alloc] init];
    [self.contentView addSubview:button1];
    [button1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).offset(5);
        make.left.equalTo(self.contentView.mas_left).offset(5);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-5);
        make.width.mas_equalTo(button1.mas_height);
    }];
<<<<<<< HEAD
    [button1 setBackgroundImage:[UIImage imageNamed:@"event_qiang_cell_tag_1"] forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(button1DidClick) forControlEvents:UIControlEventTouchUpInside];
=======
    button1.backgroundColor = [UIColor redColor];
    [button1 setBackgroundImage:[UIImage imageNamed:@"about_logo"] forState:UIControlStateNormal];
>>>>>>> b46e790f68e037a72e1c63bed0aa6db322c7b047
    
    UIButton *button2 = [[UIButton alloc] init];
    [self.contentView addSubview:button2];
    [button2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(button1);
        make.left.equalTo(button1.mas_right).offset(5);
        make.right.equalTo(self.contentView.mas_right).offset(-5);
    }];
    [button2 setBackgroundImage:[UIImage imageNamed:@"event_qiang_cell_tag_1"] forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(button2DidClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *button3 = [[UIButton alloc] init];
    [self.contentView addSubview:button3];
    [button3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.width.equalTo(button2);
        make.left.equalTo(button2);
        make.bottom.equalTo(button1);
        make.top.equalTo(button2.mas_bottom).offset(5);
    }];
    [button3 setBackgroundImage:[UIImage imageNamed:@"event_qiang_cell_tag_1"] forState:UIControlStateNormal];
    [button3 addTarget:self action:@selector(button3DidClick) forControlEvents:UIControlEventTouchUpInside];
}


@end
